'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 战斗结束后的抽奖
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var battleRecordModel = require('../../../../models/battleRecordModel');
var luckyUtil = require('../../../../util/luckyUtil');
var rewardService = require('../../../../services/rewardService');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var record_id = msg.record_id;
    var position = msg.position;
    if (!record_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var battle_record_model;
    var random_reward;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                battleRecordModel.getRecordByAtkRoleId(role_id, record_id, cb);
            },
            function (model, cb) {
                battle_record_model = model;

                if (!battle_record_model) {
                    return next(null, {
                        code: Code.BATTLE_RECORD_NOT_EXIST
                    });
                }

                // 判断副本是否可以抽奖
                if (!battle_record_model.canLucky()) {
                    return next(null, {
                        code: Code.BATTLE_CAN_NOT_LUCK
                    });
                }
                cb();
            },
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'build', 'task', 'achieve', 'daily_task', 'tech', 'accrued'],
                    cb
                );
            },
            function (cb) {
                // 获取随机出来的奖励
                random_reward = battle_record_model.lucky(position);

                if (!random_reward) {
                    return next(null, {
                        code: Code.BATTLE_REWARD_ERROR
                    });
                }

                if (!!random_reward.reward_type) {
                    rewardService.addRewardToRole(player_manager, random_reward, moduleType.MDL_PVP, operationType.OPR_PVP_LUCKY);
                }

                cb();
            },
            function (cb) {
                battle_record_model.save();
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    position: position,
                    reward: random_reward
                }
            });
        }
    );
};
